# coding=utf-8
import argparse
import ctypes
import os
import subprocess
import time

from pid.decorator import pidfile


def hide_console():
    if os.name == 'nt':
        ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0)


def monitor_process(command):
    hide_console()
    while True:
        process = subprocess.Popen(command, shell=True)
        process.wait()
        time.sleep(5)  # 间隔一秒重启


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='manual to this script')
    parser.add_argument("--code", type=str, default="test")
    parser.add_argument("--host", type=str, default="platform.wechain360.com")
    parser.add_argument("--local", type=bool, default=False)
    args = parser.parse_args()

    # Create a decorated version of the function
    decorated_monitor_process = pidfile(pidname=f'weigh_python_{args.code}.pid')(monitor_process)

    command = f"python main2.py --code {args.code} --host {args.host}"
    if args.local:
        command += f" --local true"
    decorated_monitor_process(command)
